package com.moter.one.wyydemo1.backstage.controller;

import com.moter.one.wyydemo1.backstage.vo.DataVo;
import com.moter.one.wyydemo1.entity.UserInfo;
import com.moter.one.wyydemo1.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AxiosUserController {

    @Autowired
    private UserInfoService userInfoService;

    @ResponseBody
    @RequestMapping("/user/searchAllByPage")
    public DataVo<List<UserInfo>> searchAllByPage(Integer page,Integer limit){
        List<UserInfo> users = userInfoService.searchAllByPage((page-1)*limit,limit);
        DataVo<List<UserInfo>> dataVo = new DataVo<>(0, "查询成功", users, userInfoService.searchAllUsers().size());
        return dataVo;
    }

    @ResponseBody
    @RequestMapping("/user/search")
    public DataVo<List<UserInfo>> searchBy_account_nickname_phone(String account,String nickname,String phone){
        List<UserInfo> userInfos = userInfoService.searchBy_account_nickname_phone(account, nickname, phone);
        return new DataVo<>(0, "查询成功", userInfos, userInfos.size());
    }
    @ResponseBody
    @RequestMapping("/user/add")
    public Integer insertUser(@RequestBody UserInfo userInfo){
        return userInfoService.insertUserInfoMapper(userInfo);
    }

    @ResponseBody
    @RequestMapping("/user/delete")
    public Integer deleteByAccount(@RequestBody UserInfo userInfo){
        return userInfoService.deleteByAccount(userInfo.getAccount());
    }

    @ResponseBody
    @RequestMapping("/user/query")
    public UserInfo getUserByAccount(HttpSession session){
        String account =(String) session.getAttribute("account");
        System.out.println("account = " + account);
        return userInfoService.getUserByAccount(account);
    }
}
